package com.x.reader.controller;

import com.x.framework.Base;
import com.x.framework.model.BaseView;
import com.x.framework.model.ResData;
import com.x.reader.model.*;
import com.x.reader.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RestController
public class FavouriteController extends Base {

    @Autowired
    private FavouriteService favouriteService;

    @Autowired
    private BookService bookService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private AudioService audioService;

    @RequestMapping(value = "/InsertFavourite")
    public ResData insertFavourite(Favourite favourite) {
        int categoryId = favourite.getCategoryId();
        if (categoryId == 1) {
            Book book = new Book();
            book.setBookId(favourite.getFavouriteId());
            List<Book> bookList = bookService.selectBookList(book);
            book = bookList.get(0);
            favourite.setFavouriteId(book.getBookId());
            favourite.setFavouriteTitle(book.getBookTitle());
            favourite.setFavouriteCover(book.getBookCover());
            favourite.setCategoryId(1);
        } else if (categoryId == 2) {
            Video video = new Video();
            video.setVideoId(favourite.getFavouriteId());
            List<Video> videoList = videoService.selectVideoList(video);
            video = videoList.get(0);
            favourite.setFavouriteId(video.getVideoId());
            favourite.setFavouriteTitle(video.getVideoTitle());
            favourite.setFavouriteCover(video.getVideoCover());
            favourite.setCategoryId(2);
        } else if (categoryId == 19) {
            Audio audio = new Audio();
            audio.setAudioId(favourite.getFavouriteId());
            List<Audio> audioList = audioService.selectAudioList(audio);
            audio = audioList.get(0);
            favourite.setFavouriteId(audio.getAudioId());
            favourite.setFavouriteTitle(audio.getAudioTitle());
            favourite.setFavouriteCover(audio.getAudioCover());
            favourite.setCategoryId(19);
        }

        favourite.setFavouriteCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
        favouriteService.insertFavourite(favourite);
        ResData resData = new ResData(1);
        return resData;
    }

    @RequestMapping(value = "/DeleteFavourite")
    public ResData deleteFavourite(Favourite favourite) {
        int flag = favouriteService.deleteFavourite(favourite);
        ResData resData = new ResData(1);
        return resData;
    }

    @RequestMapping(value = "/SelectFavouriteList")
    public ResData selectFavouriteList(Favourite favourite) {
        List<Favourite> favouriteList = favouriteService.selectFavouriteList(favourite);
        ResData resData = new ResData(1);
        resData.setResult(favouriteList);
        return resData;
    }

}
